﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Rnders the Publish Date of Current or Containg Post
    /// </summary>
    public class PostPublishFrom : BasePostPropertyControl
    {
        public PostPublishFrom()
        {
            UnpublishedText = "Unpublished";
            Format = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.FullDateTimePattern;
        }

        public override void DataBind()
        {
            var post = FindCurrentPost();
            if (post != null && post.PublishFrom != null)
            {
                if (post.PublishFrom.HasValue)
                    Text = post.PublishFrom.Value.ToString(Format);
                else
                    Text = UnpublishedText;
            }
            else
            {
                Text = DateTime.Now.ToString(Format);
            }
            base.DataBind();
        }

        /// <summary>
        /// DateTime format to use - Defauts to System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.FullDateTimePattern;
        /// </summary>
        public string Format { get; set; }
        public string Text { get; set; }
        /// <summary>
        /// Text to play if post is unpublished - defaults to "Unpublished"
        /// </summary>
        public string UnpublishedText { get; set; }

        protected override object SaveViewState()
        {
            ViewState["Text"] = Text;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            Text = ViewState["Text"] as string;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write(Text);
        }

    }
}
